package com.yd;

import com.yd.config.Config;
import com.yd.util.FileUtils;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 提取出String.xml种的键值对，待翻译的文本放到 res->file目录中
 * 将项目中的resources标记为资源目录
 */
public class ExtractStringClient {

    public static void main(String[] args) {

        File inputFile = Config.inputFile;
        File outFile = Config.outFile;
        extractInfo(inputFile, outFile);
    }


    /**
     * 提取字符串
     *
     * @param inputFile
     * @param outFile
     */
    public static void extractInfo(File inputFile, File outFile) {
        System.out.println();
        System.out.println("extractInfo>>>path = " + inputFile.getPath());

        if (inputFile != null && inputFile.isDirectory()) {
            File[] files = inputFile.listFiles();
            extractKeyAndValue(files, outFile);
        } else {
            System.out.println("extractInfo>>>遍历文件 >>>>inputDir = " + inputFile.getPath() + "====不存在，编译一下项目Build->Build Project");
        }
    }

    /**
     * 提取文件中的key和要翻译的文本信息
     *
     * @param files
     */
    public static void extractKeyAndValue(File files[], File out) {
        Pattern pattern = Pattern.compile("<string (.+)\">");
        for (File file : files) {
            System.out.println("========================================");
            System.out.println("处理文件>>>>" + file.getPath());
            System.out.println("输出目录>>>>" + out.getPath());
            //目录的话递归
            if (file.isDirectory()) {
                File newOut = new File(out.getPath(), file.getName());
                System.out.println("下一级输出目录>>>>" + newOut.getPath());
                extractKeyAndValue(file.listFiles(), newOut);
            } else {
                FileUtils.createOrExistsDir(out);
                String parentPath = out.getPath();
                String originName = FileUtils.getFileNameNoExtension(file);
//                String originExt = com.yd.util.FileUtils.getFileExtension(file);
                String originExt = "txt";
                System.out.println("originName=" + originName + ",originExt=" + originExt);
                File keyFile = new File(parentPath, originName + "_key." + originExt);
                File txtFile = new File(parentPath, originName + "_text." + originExt);
                FileUtils.createFileByDeleteOldFile(keyFile);
                FileUtils.createFileByDeleteOldFile(txtFile);
                String str = null;
                try (BufferedReader reader = new BufferedReader(new FileReader(file));
                     BufferedWriter writerKey = new BufferedWriter((new FileWriter(keyFile)));
                     BufferedWriter writerTxt = new BufferedWriter(new FileWriter(txtFile))) {

                    while ((str = reader.readLine()) != null) {
                        str = str.trim();
                        if (str.replace(" ", "").equals("")) {
                            //空行
                            writerKey.write(str);
                            writerKey.newLine();
                            writerTxt.write(str);
                            writerTxt.newLine();
                        } else if (str.contains("<!--")) {
                            //注释行
                            writerKey.write(str);
                            writerKey.newLine();
                            writerTxt.write("");
                            writerTxt.newLine();
                        } else if (str.contains("string")) {
                            //需要解析的行
                            Matcher matcher = pattern.matcher(str);
                            if (matcher.find()) {
                                String key = matcher.group(0);
                                String text = str.replace(key, "")
                                        .replace("</string>", "")
                                        .replace("\\\'", "\'")
                                        .replace("\\n", " ");
                                System.out.println(key);
                                System.out.println(text);
                                writerKey.write(key);
                                writerKey.newLine();
                                writerTxt.write(text);
                                if (str.contains("string>")) {
                                    writerTxt.newLine();
                                }
                            } else if (str.contains("string>")) {
                                //文本中间换行了，类似如下
//                                <string name="rc_sight_download_failed">Failed to download,
//                                tap to download again</string>
                                String text = str
                                        .replace("</string>", "")
                                        .replace("\\\'", "\'")
                                        .replace("\\n", " ");
                                System.out.println(text);
                                writerTxt.write(text);
                                writerTxt.newLine();
                            }
                        }
                    }
                    writerKey.flush();
                    writerTxt.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
